<template>
  <div>
    <span>姓名：</span><el-input v-model="username" placeholder="请输入姓名" style="width: 60%;"></el-input><el-button type="primary" style="margin-left: 8px;" @click="query">搜索</el-button><el-button type="primary" style="margin-left: 8px;" @click="showAdd">新增</el-button>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="username"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="mail" 
        label="邮箱"
        width="180">
      </el-table-column>
      <el-table-column
        prop="phone"
        label="电话">
      </el-table-column>
      <el-table-column
        prop="age"
        label="年龄">
      </el-table-column>
    </el-table>
    <el-dialog
  title="新增"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <el-form ref="form" :model="form" label-width="80px">
  <el-form-item label="姓名">
    <el-input v-model="form.name"></el-input>
  </el-form-item>
    <el-form-item label="邮箱">
    <el-input v-model="form.name"></el-input>
  </el-form-item>
    <el-form-item label="手机号">
    <el-input v-model="form.name"></el-input>
  </el-form-item>
    <el-form-item label="年龄">
    <el-input v-model="form.name"></el-input>
  </el-form-item>
</el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="insertUser">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>
<script>
import {getUserAll,getUserByName,addUser} from '../api/data.js'
export default {
    name:'page2',
    data() {
        return {
          tableData: [],
          username:'',
          dialogVisible: false,
          form: {
          username: '',
          phone: '',
          age: '',
          mail: '',
        }
        }
      },
      mounted(){
        getUserAll().then(res=>{
          console.log("用户信息查询：",res)
          this.tableData = res.data
        })
      },
      methods:{
        query(){
          if(this.username==""){
            getUserAll().then(res=>{
              console.log("通用户信息查询",res)
              this.tableData=[]
              this.tableData=res.data
            })
          }
          else{
        getUserAll().then
          getUserByName({username:this.username}).then(res=>{
            console.log("通过名称查询：",res)
              this.tableData=[]
              this.tableData.push(res.data)
          })
        }
      },
      showAdd(){
    this.dialogVisible = true
      },
       handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      insertUser(){
        addUser(this.form).then(res=>{
          console.log("新增用户",res)
          if(res.data=="新增成功"){
     this.$message.success("新增成功")
      getUserAll().then(res=>{
          console.log("获取全部用户",res)
          this.tableData = res.data
        })
          }
          else{
this.$message.console.error;("新增失败")
          }
          this.dialogVisible=false
        })
      }
}
}
</script>
<style>
</style> 